import easygui as g
import time
import pymongo
import datetime

clien=pymongo.MongoClient(host='自己服务器的IP')
db=clien.books
coll=db.data


def Billing_system():


    nowTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    msg = "请填写下面账目(点击OK提交数据)"
    title = "记账管理软件"
    fieldNames = [" *记账名字", "*进账数目", "*出账数目", '*消费用途','*进账来源','*账目生成时间']
    fieldValues = []
    fieldValues = g.multenterbox(msg, title, fieldNames)

    while 1:
        if fieldValues == None:
            break
        errmsg = ""
        for i in range(len(fieldNames)):
            option = fieldNames[i].strip()
            if fieldValues[i].strip() == "" and option[0] == "*":
                errmsg += ('【%s】为必填项。\n\n' % fieldNames[i])
        if errmsg == "":
            break
        fieldValues = g.multenterbox(errmsg, title, fieldNames, fieldValues)

    # print("用户资料如下：%s" % str(fieldValues))
    cute = {
        '记账名字': fieldValues[0],
        '进账数目': fieldValues[1],
        '出账数目': fieldValues[2],
        '消费用途': fieldValues[3],
        '进账来源': fieldValues[4],
        '账目生成时间': fieldValues[5]
    }
    coll.insert_one(cute)
    g.msgbox("数据提交成功", ok_button="确定")

    print(cute)

def Audit_system(Query_conditions):
    a=input('请输入参数：')
    for i in coll.find({Query_conditions: {'$regex': a, '$options': '$i'}}):
        #print(i)

        s = '记账名字：' + i['记账名字']+'\n' #+ ' ' + '进账数目：' + i['进账数目'] + ' ' + '出账数目：' + i['出账数目'] + ' ' + '消费用途：' + i['消费用途']+' ' +'进账来源:'+i['进账来源']+' ' +'账目生成日期时间：'+i['账目生成时间']
        s1='进账数目：' + i['进账数目']+'\n'
        s2='出账数目：' + i['出账数目']+'\n'
        s3='消费用途：' + i['消费用途']+'\n'
        s4='进账来源:'+i['进账来源']+'\n'
        s5='账目生成日期时间：'+i['账目生成时间']+'\n'
        g.msgbox((s,s1,s2,s3,s4,s5), ok_button="确定")
        #time.sleep(5)
while True:
    choose=g.buttonbox("*欢迎使用记账系统*", choices=['进入记账系统', '进入查账系统', '退出'])
    if choose=='进入记账系统':
        Billing_system()
    elif choose=='进入查账系统':
        print('*'*30)
        print('请输入查账要求如根据(记账名字查账等)')
        print('1.根据名字查账')
        print('2.根据进账数目查账')
        print('3.根据出账数据查账')
        print('4.根据消费用途查账')
        print('5.根据进账时间查账')
        print('6.根据进账来源查账')
        print('*'*30)
        Conditions_of_the_query=input('请输入对应的参数:')
        if Conditions_of_the_query =='1':

            Audit_system('记账名字')
        elif Conditions_of_the_query =='2':
            Audit_system('进账数目')

        elif Conditions_of_the_query == '3':
            Audit_system('出账数目')

        elif Conditions_of_the_query == '4':
            Audit_system('消费用途')

        elif Conditions_of_the_query == '5':
            Audit_system('账目生成时间')
        elif Conditions_of_the_query == '6':
            Audit_system('进账来源')

    else:
        break